package com.fbt.energy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbt.energy.dao.AmmeterDao;
import com.fbt.energy.entity.Ammeter;
import com.fbt.energy.entity.Equipment;
import com.fbt.energy.dao.EquipmentDao;
import com.fbt.energy.service.EquipmentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 设备表(Equipment)表服务实现类
 *
 * @author makejava
 * @since 2022-01-08 10:51:10
 */
@Service("equipmentService")
public class EquipmentServiceImpl extends ServiceImpl<EquipmentDao, Equipment> implements EquipmentService {
    @Resource
    private EquipmentDao equipmentDao;
    @Resource
    private EquipmentService equipmentService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Equipment queryById(Integer id) {
        return this.equipmentDao.queryById(id);

    }

    /**
     * 分页查询
     *
     * @param equipment 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Equipment> queryByPage(Equipment equipment, PageRequest pageRequest) {
        long total = this.equipmentDao.count(equipment);
        return new PageImpl<>(this.equipmentDao.queryAllByLimit(equipment, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param equipment 实例对象
     * @return 实例对象
     */
    @Override
    public Equipment insert(Equipment equipment) {
        this.equipmentDao.insert(equipment);
        return equipment;

    }

    /**
     * 修改数据
     *
     * @param equipment 实例对象
     * @return 实例对象
     */
    @Override
    public Equipment update(Equipment equipment) {
        this.equipmentDao.update(equipment);
        return this.queryById(equipment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.equipmentDao.deleteById(id) > 0;
    }
}
